package pong.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import pong.Main;
import pong.lan.Run;
import pong.rmi.ComRemota;

public class PongGame extends JPanel implements KeyListener, Runnable, MouseMotionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -463888109190859547L;
	private Color colorCancha;
	private ComRemota rival;
	private Font f;
	private String name1, name2;
	private boolean esRetador;
	
	private int y1, y2, dy, rx, ry, step;
	private Rectangle ball;
	
	private int max = 20;
	
	public PongGame (ComRemota host, boolean esRetador){
		rival = host;
		if(host == null){
			name1 = "Jugador 1";
			name2 = "Jugador 2";
		}else{
			name1 = Main.username.getText();
			try{name2 = host.getUsername();
			}catch(Exception e){}
		}
		this.esRetador = esRetador;
		if(esRetador){
			String aux = name1;
			name1 = name2;
			name2 = aux;
		}
		colorCancha = new Color(23,30,74);
		f = new Font("Arial",Font.ITALIC | Font.BOLD, 24);
		setBackground(Color.gray);
		
		setFocusable(true);
		addKeyListener(this);
		addMouseMotionListener(this);
		y1 = y2 = 250;
		dy = 10;
		rx = next();
		ry = next();
		
		ball = new Rectangle(440, 278, 20, 20);
		new Thread(this).start();
	}
	private int next(){
		return (Math.random() > 0.5? 1: -1);
	}
	

	@Override
	public void paintComponent (Graphics g){
		super.paintComponent(g);
		g.setColor(colorCancha);
		g.fillRect(50, 50, 800, 480);
		g.setColor(Color.white);
		g.fillRect(50, 50, 800, 10);//Raya Superior del Webo
		g.fillRect(50, 50, 10, 480);//Raya Izquierda del Webo
		g.fillRect(850, 50, 10, 480);//Raya Derecha del Webo
		g.fillRect(50,285,800,5);//raya del medio
		g.fillRect(50, 520, 800, 10);//Inferior
		g.fillRect(445, 50, 10, 480);//Medio
		g.setColor(Color.gray);
		
		g.fillRect(65,y1,15,85);// Raqueta Izquierda
		g.fillRect(830,y2, 15, 85);//Raqueta Derecha
		
		g.setColor(colorCancha);
		g.setFont(f);
		g.drawString(name1, 60, 560);
		int w = g.getFontMetrics().stringWidth(name2);
		g.drawString(name2, 850-w, 560);
		g.setColor(Color.red);
		g.fillOval(ball.x, ball.y, ball.width, ball.height);
	}
	
    public static void main(String[] args) {
		
		JFrame b = new JFrame();
		b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		PongGame pg = new PongGame(null, false);
		
		b.add(pg);
		b.setBounds(20, 50, 920, 620);
		b.setVisible(true);
	}


	@Override
	public void keyPressed(KeyEvent ev) {
		if(ev.getKeyCode() == KeyEvent.VK_UP){
			arriba();
		}else if(ev.getKeyCode() == KeyEvent.VK_DOWN){
			abajo();
		}
	}
	
	public void perform(final Run run){
		new Thread(){
			public void run(){
				try {
					run.run(rival);
				} catch (Exception e) {}
			}
		}.start();
	}

	public void abajo() {
		if(esRetador){
			y2+=dy;
			if(y2 > 435) y2 = 435;
			repaint();
		}else{
			y1+=dy;
			if(y1 > 435) y1 = 435;
			repaint();
		}
		perform(new Run() {
			@Override
			public void run(ComRemota com) throws Exception {
				com.abajo();
			}
		});
	}

	public void arriba() {
		if(esRetador){
			y2-=dy;
			if(y2 < 60) y2 = 60;
			repaint();
		}else{
			y1-=dy;
			if(y1 < 60) y1 = 60;
			repaint();
		}
		perform(new Run() {
			@Override
			public void run(ComRemota com) throws Exception {
				com.arriba();
			}
		});
	}


	@Override
	public void keyReleased(KeyEvent arg0) {}

	@Override
	public void keyTyped(KeyEvent arg0) {}

	public void arriba1() {
		if(!esRetador){
			y2-=dy;
			if(y2 < 60) y2 = 60;
			repaint();
		}else{
			y1-=dy;
			if(y1 < 60) y1 = 60;
			repaint();
		}
	}

	public void abajo1() {
		if(!esRetador){
			y2+=dy;
			if(y2 > 435) y2 = 435;
			repaint();
		}else{
			y1+=dy;
			if(y1 > 435) y1 = 435;
			repaint();
		}
	}
	
	private void setDir(int x, int py){
		if(ball.y <= py+85 && ball.y+ball.height >= py){
			rx = -rx;
			ball.x = x;
			step++;
			if(step == max)
				step = max-1;
		}
	}
	public void run(){
		while(true){
			try{
				Thread.sleep(max-step);
				ball.x += rx;
				ball.y += ry;
				
				if(ball.x < 80){
					setDir(80, y1);
				}
				if(ball.x +ball.width > 830){
					setDir(810, y2);
				}
				if(ball.x + ball.width > 850){
					rx = -rx;
					ball.x = 830;
				}
				if(ball.x < 60){
					rx = -rx;
					ball.x = 60;
				}
				if(ball.y + ball.height > 520){
					ry = -ry;
					ball.y = 500;
				}
				if(ball.y < 60){
					ry = -ry;
					ball.y = 60;
				}
				
				repaint();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		int py = e.getY()- 42;
		
		if(py >= 60 && py <= 435){
			if(esRetador){
				y2 = py;
			}else
				y1 = py;
			//
		}
	
	}
	@Override
	public void mouseMoved(MouseEvent arg0) {}
}
